package com.kugou.test.helper;

import java.io.File;

/**
 * Created by sijiluo on 2016/6/23.
 */
public class JniCoverage {
    private JniCoverage() {}
    private static JniCoverage mJniCover=null;

    /**
     * 递归删除文件和文件夹
     * @param file    要删除的根目录
     */
    public static void RecursionDeleteFile(File file){
        if(file.isFile()){
            file.delete();
            return;
        }
        if(file.isDirectory()){
            File[] childFile = file.listFiles();
            if(childFile == null || childFile.length == 0){
                file.delete();
                return;
            }
            for(File f : childFile){
                RecursionDeleteFile(f);
            }
            file.delete();
        }
    }


    public static JniCoverage getInstance() {
        if (mJniCover == null) {
            mJniCover = new JniCoverage();
            System.loadLibrary("jnicoverage");
        }
        return mJniCover;
    }
    private native  void init_native(String outPath);

    public void init(String outPath){
        File filePath = new File(outPath);
        if (filePath.exists()) {
            JniCoverage.RecursionDeleteFile(filePath);
        }
        filePath.mkdirs();
        this.init_native(outPath);
    }

    public native void flush_cov();
}
